<?php
	require_once('define.php');
	// Autoload 自动载入
	require ROOT.DS.'vendor'.DS.'autoload.php';
	//数据库导入连接
	use Illuminate\Database\Capsule\Manager as DbManager;
	// Eloquent ORM
	$capsule = new DbManager;
	$capsule->addConnection(require 'db.php');
	// 设置全局静态可访问
	$capsule->setAsGlobal();	
	$capsule->bootEloquent();
	
	/* 自动加载类 */
    function autoLoadClass($class) {
		$class = str_replace('\\',DS,$class);
		if (file_exists(ROOT . DS .$class.'.class.php')) {
            require_once(ROOT . DS .$class.'.class.php');
        }else{
		   echo '<br/>'.ROOT.DS.$class.'.class.php is not exist.';
		   exit;
		}
	}
	spl_autoload_register('autoLoadClass');
	
	//全局异常
	function gloabException($e){
		try {
			//输出当前异常
			if(!IS_DEBG){
				$ex = $e->__toString();
				if(strpos($ex,'SQLSTATE[23000]')>0){
					$info = new stdClass();
					$info->msg = '数据存在唯一约束,无法插入数据！';
					$info->success= false;
					print(json_encode($info));
				}else{
					$info = new stdClass();
					$info->msg = '未知错误，请重试或联系相关人员！';
					$info->success= false;
					print(json_encode($info));
				}
			}else{
				echo '<br/>'.$e->__toString();
			}
		}
		catch (Exception $e) {
			echo "<br/>data rollBack fail.<br/>".$e->__toString();
		}
	}
	set_exception_handler('gloabException');  